import 'package:freezed_annotation/freezed_annotation.dart';

class SafeDoubleConverter extends JsonConverter<double, dynamic> {
  final double defaultValue;

  const SafeDoubleConverter({this.defaultValue = 0.0});

  @override
  double fromJson(dynamic json) {
    if (json is double) {
      return json;
    }
    if (json is String) {
      try {
        return double.parse(json);
      } catch (e) {
        return defaultValue;
      }
    }
    return defaultValue;
  }

  @override
  dynamic toJson(double object) {
    return object;
  }
}
